<?php

namespace App\Utils;

use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\TOC;

/**
 * Word 模板管理工具类
 * Author: Albert3306
 * Date: 2020/8/3
 * @package App\Utils
 */
class WordTemplateUtils
{
    /**
     * 部位天气数据导出模板
     * Author: Albert3306
     * Date: 2020/8/3
     * @param $data
     * @throws Exception
     */
    public static function getTemplate01($data, $filename)
    {
        // 创建导出模板，并渲染数据
        $phpWord = new PhpWord();
        $phpWord->setDefaultFontName('宋体');
        $phpWord->setDefaultFontSize(12);

        // 添加页面
        $section = $phpWord->createSection();

        // 默认样式
        $titleFStyle = [
            'size' => 20,
            'bold' => true,
        ];
        $section->addText('同条件养护试件温度记录', $titleFStyle);

        // 创建表格
        $styleTable = [
            'borderSize' => 6,
            'cellMargin' => 50,
        ];
        $phpWord->addTableStyle('myTable', $styleTable);
        $table = $section->addTable('myTable');
        $table->addRow(400); // 行高400
        $table->addCell(2000)->addText('学号');
        $table->addCell(2000)->addText('姓名');
        $table->addCell(2000)->addText('专业');
        $table->addRow(400); // 行高400
        $table->addCell(2000)->addText('2015123');
        $table->addCell(2000)->addText('小明');
        $table->addCell(2000)->addText('计算机科学与技术');
        $table->addRow(400); // 行高400
        $table->addCell(2000)->addText('2016789');
        $table->addCell(2000)->addText('小傻');
        $table->addCell(2000)->addText('教育学技术');

        $writer = IOFactory::createWriter($phpWord);
        $writer->save('./download/word/' . $filename);
    }
}
